import request from 'umi-request';
import moment from 'moment';

export async function querySignContractList(params,index) {
  return request('/hr-ssc/api/api-admin/contract/getContractMangerList', {
    method: 'post',
    data: {
      pageNum: params.current,
      pageSize: params.pageSize,
      contractStatus:index,
      ...params
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function queryRenewContractList(params,index) {
  return request('/hr-ssc/api/api-admin/contract/getContractRenewList', {
    method: 'post',
    data: {
      pageNum: params.current,
      pageSize: params.pageSize,
      contractStatus:index,
      ...params
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function queryDismissContractList(params,index,endType) {
  return request('/hr-ssc/api/api-admin/contract/getTerminateList', {
    method: 'post',
    data: {
      pageNum: params.current,
      pageSize: params.pageSize,
      contractStatus:index,
      ...params,
      endType:endType,
      status:parseInt(params.status),
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function getRecordList(params) {
  return request('/hr-ssc/api/api-admin/contract/getRecordList', {
    method: 'post',
    data: {
      pageNum: params.current,
      pageSize: params.pageSize,
      ...params,
      contractEndDate:params.contractEndDate ? moment(params.contractEndDate).format('YYYY-MM-DD') : null
    },
  }).then((res) => {
    if (res.flag) {
      const data = res.data;
      return {
        current: data.currPage,
        data: data.list,
        pageSize: data.pageSize,
        success: true,
        total: data.totalCount,
      };
    } else {
      return {};
    }
  });
}

export async function getContractSubjectList() {
  return request('/hr-ssc/api/api-admin/eSign/getCompanyList', {
    method: 'post',
  }).then((res) => {
    return res.data;
  });
}

export async function getRelativeRecordsInfo(oid) {
  return request(`/hr-ssc/api/api-admin/newEmployees/getRelativeRecords/${oid}`, {
    method: 'get',
  }).then((res) => {
    return res.data;
  });
}

export async function getContractModelList(type,value) {
  return request('/hr-ssc/api/api-admin/eSign/getContractModelList', {
    method: 'post',
    data:{
      type:type,
      kind:value
    },
    requestType: 'form'
  }).then((res) => {
    return res.data;
  });
}

export async function getEmployeeById(id) {
  return request(`/hr-ssc/api/api-admin/hrEvent/getEmployeeById/${id}`, {
    method: 'get',
  }).then((res) => {
    return res.data;
  });
}

export async function saveContractInfo(params) {
  return request('/hr-ssc/api/api-admin/eSign/sendContract', {
    method: 'post',
    data: {
      ...params,
      representativeStart: params.contractStartTime,
      representativeEnd:params.contractEndTime,
      representativeLimit:params.laborContractYears,
      workAddress:params.work,
      contractAddress:params.contract,
    },
  }).then((res) => {
    return res;
  });
}

export async function getParameterList(type) {
  return request('/hr-ssc/api/api-admin/sysSetings/getParameterList', {
    method: 'post',
    data: {
      type:type
    },
    requestType: 'form'
  }).then((res) => {
    return res.data;
  });
}

export async function deleteContract(id) {
  return request(`/hr-ssc/api/api-admin/contract/deleteContract`, {
    method: 'post',
    data: {id},
    requestType: 'form'
  }).then((res) => {
    return res;
  });
}

export async function finishContract(data) {
  return request(`/hr-ssc/api/api-admin/contract/finishContract`, {
    method: 'post',
    data: {...data}
  }).then((res) => {
    return res;
  });
}

export async function getPersonRecord(oid) {
  return request(`/hr-ssc/api/api-admin/contract/getPersonRecord`, {
    method: 'post',
    data: {oid},
    requestType: 'form'
  }).then((res) => {
    if (res.flag){
      return res.data[0];
    }
    return null;
  });
}

export async function saveTerminateContract(data) {
  return request(`/hr-ssc/api/api-admin/contract/saveTerminateContract`, {
    method: 'post',
    data: {...data}
  }).then((res) => {
    return res;
  });
}

